package com.news.news.service;

import com.news.news.dao.UserWebDao;
import com.news.news.modle.User;
import com.news.news.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class UserWebService {

    @Autowired
    UserWebDao userWebDao;


     @Autowired
     RedisTemplate redisTemplate;

     @Autowired
    JavaMailSender javaMailSender;


    public int save(User user) {
        //验证验证码是否存在以及是否正确
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();
        String yzm =  valueOperations.get(user.getAccount());
        //没有存储验证码
        if(yzm==null){
            return 0;
        }
        //存储验证码
        if(yzm.equals(user.getYzm())){
            //验证码正确 保存用户信息
            userWebDao.saveUser(user);
        }else{
            // 验证码错误
            return 1;
        }
        //验证码正确返回2
        return 2;
    }

    public User userLogin(User user) {
        User u  = userWebDao.userlogin(user);
        if(u!=null){
            if(u.getState()==0) {
                //生成token
              String token =   JWTUtil.getUserToken(u);
              u.setUserToken(token);
            }
        }
            return u;
    }

    public void createYzm(String account) {
        //验证邮箱是否注册过

        //封装邮件信息的
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("2322788040@qq.com");//发送方,不变的
        message.setTo(account);//接收方
        message.setSubject("主题：注册验证码");

        //随机生成6位数字验证码
        Random random = new Random();
        int randomNum = random.nextInt(1000000);//0-1000000
        String randomCode = String.format("%06d", randomNum);//六位随机验证码

        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();
        //用邮箱作为key,验证码作为值,将其在redis中存储5分钟
        valueOperations.set(account,randomCode,5, TimeUnit.MINUTES);

        message.setText("验证码内容是:"+randomCode);//邮件内容
        javaMailSender.send(message);
    }

    public User findUserByAccount(String account) {
     User user = userWebDao.findUserByAccount(account);
     return user;
    }

    //忘记密码
    public String sendPsw(String account, String name) {
       User user;
        try {
            user =  userWebDao.sendPsw(account,name);
            //封装邮件信息的
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("2322788040@qq.com");//发送方,不变的
            message.setTo(account);//接收方
            message.setSubject("主题：账号密码");
            message.setText("您的密码是:"+user.getPassword());//邮件内容
            javaMailSender.send(message);
        }catch (Exception e){
            return "账号或昵称错误！";
        }
        return "发送密码成功！";
    }
}
